package schoolsource.model;

import java.util.regex.Pattern;

import schoolsource.sql.DBSQLAccessor;

/**
 *
 * @author Steven Steele
 * Copyright 2003
 *
 * Item containing a boolean value, database tools for saving and loading it, its required-ness,
 * and validation tools.
 */
public class TimeOfDayItem extends Item {

	// The time stored in this Item
	private String value;
	private static final Pattern FORMAT = Pattern.compile("(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])");

	/**
	 * Returns a new Item with the database field name, value, and required-ness set
	 * to the parameter values.
	 * @param newField The name of the database field where the boolean value is stored.
	 * @param newVal The value of the time. This is in the format HH:MM:SS where HH is 00-23, MM is
	 * 00-59, and SS is 00-59.
	 * @param newReq Whether the boolean value may be unset. This parameter is meaningless, since
	 * the value is always either true or false.
	 */
	public TimeOfDayItem(String newField, String newVal, boolean newReq) {
		setFieldName(newField);
		setValue(newVal);
		setRequired(newReq);
	}

	/**
	 * Sets the value using a string.
	 * @param newValue The value to set the boolean. The value of the Item is set
	 * to true if 'true' is passed in (case-insensitive) or false otherwise.
	 */
	public void setValue(String newValue) {
		setTrueValue(newValue);
		validate();
	}

	/**
	 * Sets the value using a Boolean object.
	 * @param newTrueValue An object that can be cast to a Boolean.
	 */
	public void setTrueValue(Object newTrueValue) {
		if (newTrueValue == null) {
			value = "";
		} else {
			value = (String) newTrueValue;
		}

		validate();
	}

	/**
	 * Returns the string representation of the Item.
	 * @return The string representation of the Item.
	 */
	public String toString() {
		String returnVal = "";
		if (value != null) {
			returnVal = value.toString();
		}
		return returnVal;
	}

	/**
	 * Returns the Boolean object associated with the Item.
	 * @return The Boolean object associated with the Item.
	 */
	public Object getObject() {
		return value;
	}

	/**
	 * Returns a string formatted in the appropriate manner for a boolean value,
	 * according to the DBSQLAccessor passed in.
	 * @param dba A DBSQLAccessor that defines how values are represented in the
	 * target database.
	 */
	public String getDBFormattedString(DBSQLAccessor dba) {
		return dba.getDBFormattedBoolean(toString());
	}

	/**
	 * Sets the value of the Item according to the interpretation of a database representation
	 * of the Item using the passed-in DBSQLAccessor to interpret it.
	 * @param dbString The database representation of the Item.
	 * @param dba The DBSQLAccessor that defines how values are translated from the database
	 * representation to the Item value.
	 */
	public void createFromDBString(String dbString, DBSQLAccessor dba) {
		setValue(dba.removeDBTagsFromBoolean(dbString));
	}

	/**
	 * Indicates whether the Item is valid. This value is current only after the Item
	 * has validate() called on it.
	 * @return A boolean indicating whether the Item is valid.
	 */
	public boolean isValid() {
		return valid;
	}

	/**
	 * Processes the Item and determines whether it is valid strictly according to rules
	 * governing the Item implementation and whether the Item is required.
	 */
	public void validate() {
		if (value == null && isRequired()) {
			setErrorString("This field is required and must be either \"true\" or \"false\".");
			setValid(false);
		}
		else if (!FORMAT.matcher(value).matches()) {
			setValid(false);
		}
		else {
			setValid(true);
		}
	}

	public static void main(String args[]) {
//		System.out.println("Testing");
		TimeOfDayItem todi1 = new TimeOfDayItem("", "00:00:00", true);
		todi1.validate();
		if (todi1.isValid()) {
//			System.out.println("Test 1 passed");
		}
		else {
//			System.out.println("Test 1 failed");
		}
		TimeOfDayItem todi2 = new TimeOfDayItem("", "23:59:59", true);
		todi2.validate();
		if (todi2.isValid()) {
//			System.out.println("Test 2 passed");
		}
		else {
//			System.out.println("Test 2 failed");
		}
		TimeOfDayItem todi3 = new TimeOfDayItem("", "24:00:00", true);
		todi3.validate();
		if (!todi3.isValid()) {
//			System.out.println("Test 3 passed");
		}
		else {
//			System.out.println("Test 3 failed");
		}
		TimeOfDayItem todi4 = new TimeOfDayItem("", "19:59:59", true);
		todi4.validate();
		if (todi4.isValid()) {
//			System.out.println("Test 4 passed");
		}
		else {
//			System.out.println("Test 4 failed");
		}
	}
}